package org.example.service.handleJudgeResultImpl;

import cn.hutool.json.JSONUtil;
import org.example.fegin.testBankFegin;
import org.example.pojo.Response.JudgeResult;
import org.example.pojo.Submissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 异步处理保存判题结果记录
 */
@Component
public class AsyncSaveJudge {

    @Autowired
    private testBankFegin testBankFegin;


    private final  ExecutorService executor = Executors.newCachedThreadPool() ;

    public void save(Submissions submissions, List<JudgeResult> list){

        Map<Submissions,List<JudgeResult>> map=new HashMap<>();

        map.put(submissions, list);
        //判断是否为空
        if (submissions == null || list == null) {
            throw new IllegalArgumentException("submissions and list cannot be null");
        }

        //异步提交执行任务
        Future<Boolean> submit = executor.submit(() -> testBankFegin.SaveJudge(JSONUtil.toJsonStr(map)));

        //TODO 异常处理



    }


}
